<template>
  <div style="margin-top: 50px">
    <el-form
      :model="value"
      :rules="rules"
      ref="productInfoForm"
      label-width="120px"
      class="form-inner-container"
      size="small"
    >
      <el-form-item label="名称：" prop="metaName">
        <el-input v-model="value.metaName"></el-input>
      </el-form-item>

      <el-form-item label="简单名称：" prop="metaSimpleName">
        <el-input v-model="value.metaSimpleName"></el-input>
      </el-form-item>

      <el-form-item label="背景图片：" prop="backgroundImgUrl">
        <upload
          v-model="value.backgroundImgUrl"
          accept="image/*"
          list-type="picture-card"
          :multiple="false"
          :max-count="1"
          class="bg-img-upload"
        >
        </upload>
      </el-form-item>

      <el-form-item label="类型：" prop="metaType">
        <el-select
          v-model="value.metaType"
          @change="handleBrandChange"
          placeholder="请选择类型"
          style="width: 100%"
        >
          <el-option
            v-for="item in metaTypeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="供应商：" prop="supplierId">
        <select-scroll
          v-model="value.supplierId"
          keyword-name="name"
          :api="fetchSupplierList"
          :option="{
            label: 'supplierName',
            value: 'id',
          }"
          style="width: 100%"
        />
      </el-form-item>

      <el-form-item label="单次限额(元)：" prop="singleLimit">
        <el-input-number
          v-model="value.singleLimit"
          style="width: 100%"
          :controls="false"
        ></el-input-number>
      </el-form-item>

      <el-form-item label="成本价(元)：" prop="price">
        <el-input-number
          v-model="value.price"
          style="width: 100%"
          :controls="false"
        ></el-input-number>
      </el-form-item>

      <el-form-item
        label="理赔额度(元)："
        prop="claimAmount"
        v-if="isInsurance"
      >
        <el-input-number
          v-model="value.claimAmount"
          style="width: 300px"
          :controls="false"
        ></el-input-number>
      </el-form-item>

      <el-form-item label="起始年龄：" prop="metaName">
        <el-row :gutter="16" type="flex">
          <el-col :span="12">
            <!--开始年龄-->
            <el-input-number
              v-model="value.startPurchaseAge"
              style="width: 100%"
              :controls="false"
            ></el-input-number>
          </el-col>
          <el-col :span="12">
            <el-select
              v-model="value.startUnit"
              @change="handleBrandChange"
              placeholder="请选择类型"
              style="width: 100%"
            >
              <el-option
                v-for="item in dateUnitOptions"
                :key="item.value"
                :label="item.label"
                :value="Number(item.value)"
              >
              </el-option>
            </el-select>
          </el-col>
        </el-row>
      </el-form-item>

      <el-form-item label="结束年龄：" prop="endPurchaseAge">
        <el-row :gutter="16" type="flex">
          <el-col :span="12"
            ><!--结束年龄-->
            <el-input-number
              v-model="value.endPurchaseAge"
              style="width: 100%"
              :controls="false"
            ></el-input-number>
          </el-col>
          <el-col :span="12">
            <el-select
              v-model="value.endUnit"
              @change="handleBrandChange"
              placeholder="请选择类型"
              style="width: 100%"
            >
              <el-option
                v-for="item in dateUnitOptions"
                :key="item.value"
                :label="item.label"
                :value="Number(item.value)"
              >
              </el-option>
            </el-select>
          </el-col>
        </el-row>
      </el-form-item>

      <el-form-item label="表单类型：" prop="formType">
        <el-select
          v-model="value.formType"
          @change="handleBrandChange"
          placeholder="请选择表单类型"
          style="width: 100%"
        >
          <el-option
            v-for="item in formTypeOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="链接地址：" prop="formUrl">
        <el-input v-model="value.formUrl"></el-input>
      </el-form-item>

      <el-form-item label="备注：" prop="remark">
        <el-input v-model="value.remark"></el-input>
      </el-form-item>

      <el-form-item
        label="赠险文件："
        prop="insuranceFileUrl"
        v-if="isInsurance"
      >
        <upload
          :multiple="false"
          v-model="value.insuranceFileUrl"
          accept=".pdf"
          list-type="text"
        />
        <!--<single-file-upload-->
        <!--  v-model="value.insuranceFileUrl"-->
        <!--&gt;</single-file-upload>-->
      </el-form-item>

      <el-form-item style="text-align: center">
        <el-button type="primary" size="medium" @click="handleSave"
          >提交</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { dateUnits, formTypes, metaTypes } from "@/utils/constant/pms";
import SingleFileUpload from "@/components/Upload/singleFileUpload.vue";
import { fetchSupplierList } from "@/api/supplier";
import SelectScroll from "@/components/SelectScroll.vue";
import Upload from "@/components/Upload/index.vue";
import SingleUpload from "@/components/Upload/singleUpload.vue";

export default {
  name: "ProductInfoDetail",
  components: { SingleUpload, Upload, SelectScroll, SingleFileUpload },
  props: {
    value: Object,
    isEdit: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      metaTypeOptions: metaTypes.toKeyValueArray(),
      dateUnitOptions: dateUnits.toKeyValueArray(),
      formTypeOptions: formTypes.toKeyValueArray(),
      hasEditCreated: false,
      //选中商品分类的值
      selectProductCateValue: [],
      productCateOptions: [],
      brandOptions: [],
      rules: {
        name: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
          {
            min: 2,
            max: 140,
            message: "长度在 2 到 140 个字符",
            trigger: "blur",
          },
        ],
        subTitle: [
          { required: true, message: "请输入商品副标题", trigger: "blur" },
        ],
        productCategoryId: [
          { required: true, message: "请选择商品分类", trigger: "blur" },
        ],
        brandId: [
          { required: true, message: "请选择商品品牌", trigger: "blur" },
        ],
        description: [
          { required: true, message: "请输入商品介绍", trigger: "blur" },
        ],
        requiredProp: [
          { required: true, message: "该项为必填项", trigger: "blur" },
        ],
      },
      supplierList: [],
      supplierListLoading: false,
      hasMoreSupplier: true,
      fetchSupplierList,
    };
  },
  created() {
    this.getProductCateList();
    // this.getSupplierList();
  },
  computed: {
    //商品的编号
    productId() {
      return this.value.id;
    },
    isInsurance() {
      return (
        metaTypes.getValue(this.value.metaType) &&
        metaTypes.getValue(this.value.metaType).indexOf("保险") > -1
      );
    },
  },
  watch: {
    productId: function (newValue) {
      if (!this.isEdit) return;
      if (this.hasEditCreated) return;
      if (newValue === undefined || newValue == null || newValue === 0) return;
      this.handleEditCreated();
    },
    selectProductCateValue: function (newValue) {
      if (newValue != null && newValue.length === 2) {
        this.value.productCategoryId = newValue[1];
        this.value.productCategoryName = this.getCateNameById(
          this.value.productCategoryId,
        );
      } else {
        this.value.productCategoryId = null;
        this.value.productCategoryName = null;
      }
    },
  },
  methods: {
    //处理编辑逻辑
    handleEditCreated() {
      if (this.value.productCategoryId != null) {
        this.selectProductCateValue.push(this.value.cateParentId);
        this.selectProductCateValue.push(this.value.productCategoryId);
      }
      this.hasEditCreated = true;
    },
    getProductCateList() {},
    handleSave() {
      this.$emit("finishCommit", this.isEdit);
    },
    getCateNameById(id) {
      let name = null;
      for (let i = 0; i < this.productCateOptions.length; i++) {
        for (let j = 0; j < this.productCateOptions[i].children.length; j++) {
          if (this.productCateOptions[i].children[j].value === id) {
            name = this.productCateOptions[i].children[j].label;
            return name;
          }
        }
      }
      return name;
    },
    handleNext(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit("nextStep");
        } else {
          this.$message({
            message: "验证失败",
            type: "error",
            duration: 1000,
          });
          return false;
        }
      });
    },
    handleBrandChange(val) {
      let brandName = "";
      for (let i = 0; i < this.brandOptions.length; i++) {
        if (this.brandOptions[i].value === val) {
          brandName = this.brandOptions[i].label;
          break;
        }
      }
      this.value.brandName = brandName;
    },
  },
};
</script>

<style scoped lang="scss">
.bg-img-upload {
  /deep/ .el-upload-list__item {
    border: none;
    width: 300px;
    height: auto;

    img {
      display: block;
    }
  }
}
</style>
